import { Text } from "react-native";
import AsyncStorage from "@react-native-community/async-storage";
import Theme from "../styles/Theme";
import Storage from "react-native-storage";
import { hex_md5 } from "../utils/md5";

Text.defaultProps = Object.assign({}, Text.defaultProps, { allowFontScaling: false });


export const storage = new Storage({
  size: 1000,
  storageBackend: AsyncStorage,
  defaultExpires: null,
  enableCache: false,
});
global.storage = storage;
global.channelId = "606";
global.tenantId = "";
global.token = "";
global.ws = null;
global.SOCKET_API = "";
global.Secret = "GJ9k87JcOiJ7e7JhbNpJ9e43",
global.netState = "正常";

let gval = {
  chooseRoomBack: false,
  secondTimer: null,
  logout: false,
  logging: false,
  homeGameListTimer: null,
  oneGameListTimer: null,
  currentScreen: null,
  currentProps: {},
  bestProxyItem: null,
  rws: null,
};
export { gval, Theme };
/**
 * 日志
 * @param params
 */
const log = (...params) => { // 全局Log
  // if (GLOBAL.__DEV__) {
  console.log(params);
  // }
};
global.Log = log;


global.encodeUtf8 = (s1) => {
  var s = escape(s1);
  var sa = s.split("%");
  var retV = "";
  if (sa[0] != "") {
    retV = sa[0];
  }
  for (var i = 1; i < sa.length; i++) {
    if (sa[i].substring(0, 1) == "u") {
      retV += Hex2Utf8(Str2Hex(sa[i].substring(1, 5)));
    } else retV += "%" + sa[i];
  }

  return retV;
};

global.hex2Utf8 = (s) => {
  var retS = "";
  var tempS = "";
  var ss = "";
  if (s.length == 16) {
    tempS = "1110" + s.substring(0, 4);
    tempS += "10" + s.substring(4, 10);
    tempS += "10" + s.substring(10, 16);
    var sss = "0123456789ABCDEF";
    for (var i = 0; i < 3; i++) {
      retS += "%";
      ss = tempS.substring(i * 8, (eval(i) + 1) * 8);

      retS += sss.charAt(Dig2Dec(ss.substring(0, 4)));
      retS += sss.charAt(Dig2Dec(ss.substring(4, 8)));
    }
    return retS;
  }
  return "";
};

global.dig2Dec = (s) => {
  var retV = 0;
  if (s.length == 4) {
    for (var i = 0; i < 4; i++) {
      retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
    }
    return retV;
  }
  return -1;
};

global.str2Hex = (s) => {
  var c = "";
  var n;
  var ss = "0123456789ABCDEF";
  var digS = "";
  for (var i = 0; i < s.length; i++) {
    c = s.charAt(i);
    n = ss.indexOf(c);
    digS += Dec2Dig(eval(n));

  }
  //return value;
  return digS;
};

global.dec2Dig = (n1) => {
  var s = "";
  var n2 = 0;
  for (var i = 0; i < 4; i++) {
    n2 = Math.pow(2, 3 - i);
    if (n1 >= n2) {
      s += "1";
      n1 = n1 - n2;
    } else
      s += "0";

  }
  return s;
};


global.IsNullOrEmpty = (thing) => {

  // Check for undefined, null and NaN
  if (typeof thing === "undefined" || thing === null ||
    (typeof thing === "number" && isNaN(thing))) {
    return true;
  }

  // Numbers, booleans, functions and DOM nodes are never judged to be empty
  else if (typeof thing === "number" || typeof thing === "boolean" ||
    typeof thing === "function" || thing.nodeType === 1) {
    return false;
  }

  // Check for arrays with zero length
  else if (thing.constructor === Array && thing.length < 1) {
    return true;
  }

  // Check for empty strings after accounting for whitespace
  else if (typeof thing === "string") {
    if (thing.replace(/\s+/g, "") === "") {
      return true;
    } else {
      return false;
    }
  }

  // Check for objects with no properties, accounting for natives like window and XMLHttpRequest
  else if (Object.prototype.toString.call(thing).slice(8, -1) === "Object") {
    var props = 0;
    for (var prop in thing) {
      if (thing.hasOwnProperty(prop)) {
        props++;
      }
    }
    if (props < 1) {
      return true;
    }
  }

  // If we've got this far, the thing is not null or empty
  return false;
};


global.getNowFormatDate = () => {
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + " " + date.getHours() + seperator2 + date.getMinutes()
    + seperator2 + date.getSeconds();
  return currentdate;
};

global.getNowFormatDateYMDHMS = () => {
  let date = new Date();

  let month = date.getMonth() + 1;
  let strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  const currentdate = date.getFullYear() + month + +strDate + date.getHours() + date.getMinutes() + date.getSeconds();
  return currentdate;
};

global.getNowFormatDateYMD = () => {
  var date = new Date();
  var seperator1 = "-";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
  return currentdate;
};

global.getLastWeekFormatDateYMD = () => {
  var nowdate = new Date();
  var date = new Date(nowdate - 7 * 24 * 3600 * 1000);
  var seperator1 = "-";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
  return currentdate;
};

global.getLastMonthFormatDateYMD = () => {
  var date = new Date();
  date.setMonth(date.getMonth() - 1);
  var seperator1 = "-";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
  return currentdate;
};

global.getNowFormatDateYM = () => {
  var date = new Date();
  var seperator1 = "-";
  var month = date.getMonth() + 1;
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  var currentdate = date.getFullYear() + "年" + month + "月";
  return currentdate;
};

global.checkEmail = (str) => {
  var regExp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
  if (!regExp.test(str)) {
    return false;
  }
  return true;
};

global.compareNow = (dateTime) => {
  dateTime = dateTime.replace(/-/g, "/");//替换字符，变成标准格式
  var now = new Date();//取今天的日期
  var d1 = new Date(Date.parse(dateTime));
  if (now > d1) {
    return false;//小于当前时间
  }
  return true;//大于当前时间
};


global.compareTheTime = () => {
  let thetime = "2019-06-29 23:59:59";
  let d = new Date(Date.parse(thetime.replace(/-/g, "/")));
  var curDate = new Date();
  if (curDate >= d) {
    Log("大于指定时间");
    return true;

  } else {
    Log("小于指定时间");
    return false;
  }
};

global.toLocaleString = (val) => {
  if (val) {
    if (val.toString().indexOf(",") == -1) {
      return Number(val).toLocaleString();
    } else {
      return val;
    }
  }
};

global.formatText = (val) => {
  if (val) {
    if (val.toString().indexOf("支付") !== -1) {
      return val.substring(0, val.length - 2);
    } else {
      return val;
    }
  }
};

// 转换为钱格式
global.formatMoney = (number, places, symbol, thousand, decimal) => {
  number = number / 100 || 0;
  places = !isNaN(places = Math.abs(places)) ? places : 2;
  symbol = symbol !== undefined ? symbol : "¥";
  thousand = thousand || ",";
  decimal = decimal || ".";
  var negative = number < 0 ? "-" : "",
    i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
    j = (j = i.length) > 3 ? j % 3 : 0;
  return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
};

//生成签名
global.getSign = (params) => {
  let sign = "";
  let i = 0;
  let obj = {};
  let ret = {};
  if (IsNullOrEmpty(params) || Object.keys(params).length === 0) {
    sign = global.Secret;
  } else {
    //清除内容为空的key
    for (let key in params) {
      let value = params[key];
      if (value != undefined) {
        ret[key] = value;
      }
    }
    //字典排序a-z
    const sdic = Object.keys(ret).sort();

    for (let key in sdic) {
      let value = ret[sdic[key]];
      obj[sdic[key]] = value;
      let val = null;
      if ((typeof value === "object") && (value !== null)) {
        val = JSON.stringify(value);
      } else {
        val = value;
      }
      console.log(key, sdic[key], val);
      if (Object.keys(ret).length - 1 === i) {
        sign += sdic[key] + "=" + val + global.Secret;
      } else {
        sign += sdic[key] + "=" + val + "&";
      }
      ++i;
    }
  }

  // obj['sign'] = hex_md5(sign).toUpperCase();
  logger("待签名：" + sign, "签名后:" + hex_md5(sign));
  return hex_md5(sign);
};

